<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Activity
 *
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $subject
 * @mixin \Eloquent
 * @property int $id
 * @property int $user_id
 * @property int $subject_id
 * @property string $subject_type
 * @property string $type
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereSubjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereSubjectType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Activity whereUserId($value)
 */
class Activity extends Model
{
	protected $guarded = [];

	public function subject()
	{
		return $this->morphTo();
	}

	/**
	 * @param User $user
	 * @param int  $take
	 * @return mixed
	 */
	public static function feed($user, $take = 50)
	{
		return static::where('user_id', $user->id)
			->latest()
			->with('subject')
			->take($take)
			->get()
			->groupBy(function ($activity) {
				return $activity->created_at->format('Y-m-d');
			});
	}
}